<template>
  <div v-show="show">
    <a-descriptions title="讲台详情" bordered style="text-align:center">
      <a-descriptions-item label="课程名称">
        {{ course.name }}
      </a-descriptions-item>
      <a-descriptions-item label="课程编号">
        {{ course.courseId }}
      </a-descriptions-item>
      <a-descriptions-item label="上课地点">
        {{ courseInfo.loc }}
      </a-descriptions-item>
      <a-descriptions-item label="开始时间">
        {{ courseInfo.startWeek }}
      </a-descriptions-item>
      <a-descriptions-item label="结束时间">
        {{ courseInfo.endWeek }}
      </a-descriptions-item>
      <a-descriptions-item label="任课教师">
        {{ userInfo.username }}
      </a-descriptions-item>
      <a-descriptions-item label="所属学院">
        {{ userInfo.college }}
      </a-descriptions-item>
    </a-descriptions>
    <ul>
      <span>每周课程安排</span>
      <li v-for="(item,index) in classTime" :key="index">
        {{ item }}
      </li>
      <!--      <li v-for="(item,index) in courseInfo.classTime" :key="index">-->
      <!--        第{{ index + 1 }}节课：周{{ item.day }}第{{ item.class }}节-->
      <!--      </li>-->
    </ul>
    <a-button icon="edit" @click="showChange">调停课申请</a-button>
    <a-modal :maskClosable="false" title="调停课申请" :visible="visible" :confirm-loading="confirmLoading" @ok="handleOk"
             @cancel="handleCancel">
      <a-radio-group v-model="applicationType">
        <a-radio-button value="0">
          停课
        </a-radio-button>
        <a-radio-button value="1">
          调课
        </a-radio-button>
      </a-radio-group>
      <div>
        要调整课程的教学周数：
        <a-input-number id="教学周数" v-model="changeWeek" :min="courseInfo.startWeek" :max=" courseInfo.endWeek "/>
        当前值：{{ changeWeek }}
      </div>
      <div>
        要调整的课程时间：
        <!--        <a-menu  >-->
        <!--          <a-menu-item v-for="(item,key) in classTime" :key="key" >-->
        <!--            <a href="javascript:changeclass=key;">{{item}}</a>-->
        <!--          </a-menu-item>-->
        <!--          <a-menu-divider />-->
        <!--        </a-menu>-->
        <a-radio-group v-model="selectedClass" :default-value="0">
          <a-radio-button v-for="(item,key) in classTime" :value="key" :key="key">
            {{ item }}
          </a-radio-button>
        </a-radio-group>
      </div>

      <div v-show="(applicationType==1)">
        要调整到的教学周：
        <a-input v-model="newWeek" placeholder="教学周数"></a-input>
        要调整到周几：
        <a-input v-model="newDay" placeholder="周内第几天"></a-input>
        要调整到第几节课：
        <a-input v-model="newClass" placeholder="第几节课"></a-input>
        要调整到哪个教室：
        <a-input v-model="newRoom" placeholder="教室选择"></a-input>
        要调整到哪个教学楼：
        <a-input v-model="newBuilding" placeholder="教学楼选择"></a-input>
      </div>
      调停课理由：
      <a-textarea v-model="description" placeholder="请输入调停课理由"></a-textarea>
    </a-modal>
  </div>
</template>


<script>

import {getCourseInfo} from "@/api/teacher/courseInfo"
import userInfo from "@/api/users/userInfo"
import {adjustmentClass} from "@/api/audition/transferApplication";
import {suspendClass} from "@/api/audition/transferApplication";

export default {
  name: "sessionInfo",
  props: {
    courseItem: Object,
    show: Boolean,
  },
  data() {
    return {
      course: this.courseItem,
      courseInfo: {},
      userInfo: {},
      visible: false,
      confirmLoading: false,
      classTime: [],
      changeWeek: '',
      changeClass: -1,
      description: '',
      applicationType: -1,// 调课申请类型
      selectedClass: -1,// 调课申请中选择的第几节课
      newWeek: '',
      newDay: '',
      newClass: '',
      newRoom: '',
      newBuilding: '',
    }
  },
  mounted() {
    this.getUserInfo();
    this.getCourseInfo();
  },
  methods: {
    getClassTime() {
      let res = [];
      if (this.courseInfo != null) {
        for (let i = 0; i < this.courseInfo.classTime.length; i++) {
          res.push("第" + (i + 1) + "节课：周" + this.courseInfo.classTime[i].day + "第" + this.courseInfo.classTime[i].class + "节")
        }
      }
      this.classTime = res;
    },
    async getCourseInfo() {
      let self = this;
      await getCourseInfo(self.course.courseId).then(res => {
        if (res.result) {
          self.courseInfo = res.courseInfo;
          self.getClassTime();
        } else {
          this.$message.error(res.msg);
        }
      })
    },
    getUserInfo() {
      let self = this;
      userInfo().then(res => {
        if (res.result) {
          self.userInfo = res.data;
        } else {
          this.$message.error(res.msg);
        }
      })
    },
    showChange() {
      this.visible = true;
    },
    handleOk() {
      let self = this;
      if (this.applicationType == -1) {
        this.$message.error("请选择调停课类型");
        return
      }
      if (!this.changeWeek) {
        this.$message.error("请选择调课周数");
        return
      }
      if (this.selectedClass == -1) {
        this.$message.error("请选择要调整的节数");
        return
      }
      if (!this.description) {
        this.$message.error("请输入调停课理由");
        return
      }
      if (this.applicationType == 0 && this.changeWeek && this.selectedClass != -1 && this.description) {
        this.confirmLoading = true;
        setTimeout(() => {
          this.confirmLoading = false;
        }, 2000);
        //发送请求
        suspendClass(this.course.courseId, this.applicationType, this.changeWeek, this.courseInfo.classTime[this.selectedClass].day, this.courseInfo.classTime[this.selectedClass].class, this.description).then(res => {
          if (res.result) {
            self.clear()
          } else {
            this.$message.error(res.msg)
          }
        })
        return
      }
      if (this.selectedClass == -1 || !this.newWeek || !this.newDay || !this.newClass || !this.newRoom || !this.newBuilding) {
        this.$message.error("请选择调课后的信息");
        return
      }
      if (this.applicationType == 1 && this.changeWeek && this.selectedClass != -1 && this.newWeek && this.newDay && this.newClass && this.newRoom && this.newBuilding) {
        this.confirmLoading = true;
        setTimeout(() => {
          this.confirmLoading = false;
        }, 2000);
        //发送请求
        adjustmentClass(this.course.courseId, this.applicationType, this.changeWeek, this.courseInfo.classTime[this.selectedClass].day, this.courseInfo.classTime[this.selectedClass].class, this.newWeek, this.newDay, this.newClass, this.newRoom, this.newBuilding, this.description).then(res => {
          if (res.result) {
            self.clear()
          } else {
            this.$message.error(res.msg)
          }
        })
      }
    },
    handleCancel() {
      this.clear();
    },
    clear() {
      this.applicationType = -1;
      this.selectedClass = -1;
      this.changeWeek = '';
      this.changeClass = -1;
      this.description = '';
      this.newWeek = '';
      this.newDay = '';
      this.newClass = '';
      this.newRoom = '';
      this.newBuilding = '';
      this.visible = false;
    }
  }
}
</script>

<style scoped>

</style>